$(function () {

    var init = function (params) {

        laydate.render({
            elem: '#endTime'
            , type: 'datetime'
            , format:'yyyy/MM/dd'
            , value: '2099/12/12',
            done:function () {
                $('form').bootstrapValidator('resetForm');
            }
        });

        var editor = $.createEditor({
            id:"#editor"
        });

        var $form = $('form').bootstrapValidator({
            fields: {
                cardNum: {
                    validators: {
                        regexp: {
                            regexp: /^[0-9]+$/,
                            message: '只能是数字'
                        }
                    }
                },
                price: {
                    validators: {
                        notEmpty: {
                            message: '兑换价格不能为空'
                        },
                        regexp: {
                            regexp: /^[0-9]+$/,
                            message: '只能是数字'
                        }
                    }
                },
                endTime:{
                    validators: {
                        notEmpty: {
                            message: '截止时间不能为空'
                        }
                    }
                },
                title:{
                    validators: {
                        notEmpty: {
                            message: '标题不能为空'
                        }
                    }
                }
            }
        });

        if (params) {
            $form.find('input[name=cardNum]').val(params.cardNum);
            $form.find('input[name=price]').val(params.price);
            $form.find('input[name=title]').val(params.title);
            $form.find('select[name=type]').val(params.type);
            $form.find('input[name=endTime]').val(new Date(params.endTime).format('yyyy/MM/dd'));

            editor.txt.html(params.info)
        }

        $("#submit").on("click", function () {
            var bootstrapValidator = $form.data('bootstrapValidator');

            bootstrapValidator.validate();

            if (bootstrapValidator.isValid()) {

                var param = {
                    sort:  $form.find('input[name=cardNum]').val(),
                    price:  $form.find('input[name=price]').val(),
                    title:  $form.find('input[name=title]').val(),
                    type:  $form.find('select[name=type]').val(),
                    endTime:  $form.find('input[name=endTime]').val(),
                    token: window.localStorage.getItem('token'),

                    info:editor.txt.html()
                }
                var postUrl = '/ticket/doInsert';

                if (params) {
                    param.id = params.id
                    postUrl = '/ticket/doUpdate';
                }
                var submitLayer = layer.load(0);
                $.restPost(postUrl, param).done(function (msg) {
                    layer.close(submitLayer)
                    $.rootSuccessDialog(msg, 2000, function () {
                        location.hash = '/manage/ticketmanage'
                    })

                }).fail(function (msg) {
                    $.rootErrorDialog(msg, 2000)

                })
            }

        })
    };

    if (/ticketedit/.test(location.hash)) {
        $.restPost("/ticket/doSelect", {
            token: window.localStorage.getItem('token'),
            id: $.getUrlLocation()
        }).done(function (mess, data) {
            init(data)
        })

    } else {
        init(false)

    }

})